<?php
/**
 * @Author: qi
 * @Email:   905766491@qq.com
 * @Date:   2017-02-17 12:28:34
 * @Last Modified by:   qi
 * @Last Modified time: 2017-02-23 11:11:06
 */
namespace frontend\modules\models;
use yii\db\ActiveRecord;
use Yii;
class Admin extends ActiveRecord{
    public $rememberMe = true;
    public $repass;
    public static function tableName(){
        return "shop_admin";
    }

    public function attributeLabels(){
        return [
            'adminuser'=>'管理员账号',
            'adminemail'=>'管理员邮箱',
            'adminpass'=>'管理员密码',
            'repass'=>'确认密码'
        ];
    }
    public function rules(){
        return [
            ['adminuser','required','message'=>'管理员账号不能为空','on'=>['login','seekpass','adminadd','changepass','changeemail']],
            ['adminpass','required','message'=>'密码不能为空','on'=>['login','adminadd','changepass','changeemail']],
            ['rememberMe','boolean','on'=>'login'],
            ['adminpass','validatePass','on'=>['login','changeemail']],
            ['adminemail','required','message'=>'邮箱不能为空','on'=>['seekpass','adminadd','changeemail']],
            ['adminemail','email','message'=>'邮箱格式不正确','on'=>['seekpass','adminadd','changeemail']],
            ['adminemail','unique','message'=>'邮箱已经被注册','on'=>['adminadd','changeemail']],
            ['adminuser','unique','message'=>'管理员已经被注册','on'=>['adminadd']],
            ['adminemail','validateSeekpass','on'=>'seekpass'],
            ['repass','required','message'=>'确认密码不能为空','on'=>['changepass','adminadd']],
            ['repass','compare','compareAttribute'=>'adminpass','message'=>'密码不一致','on'=>['changepass','adminadd']]
        ];



    }


    public function validatePass(){
        if(!$this->hasErrors()){
            $data = self::find()->where('adminuser= :user and adminpass = :pass', [':user'=>$this->adminuser,':pass'=>md5($this->adminpass)])->one();
            if(is_null($data)){
                $this->addError('adminpass', '用户名或密码错误');
            }
        }
    }

    public function validateSeekpass(){
        if(!$this->hasErrors()){
            $data = self::find()->where('adminuser= :user and adminemail = :email', [':user'=>$this->adminuser,':email'=>$this->adminemail])->one();
            if(is_null($data)){
                $this->addError('adminemail', '邮箱不匹配');
            }
        }
    }

    public function login($data){
        $this->scenario = "login";
        if($this->load($data) && $this->validate()){
            $lifttime = $this->rememberMe ? 24*3600 : 0;
            $session = Yii::$app->session;
            session_set_cookie_params($lifttime);
            $session['admin']=['adminuser'=>$this->adminuser,'isLogin'=>1];
            $cookie = Yii::$app->cookie;
           // $cookie->add(['name'=>'admin','value'=>['adminuser'=>$this->adminuser,'isLogin'=>1],'expire'=>time()+86400]);

            $cookie = new Yii\web\Cookie([
                'name' => 'admin',
                'expire' => time() + 3600,
                'httpOnly' => true,
                'value' => ['adminuser'=>$this->adminuser,'isLogin'=>1]
            ]);
            Yii::$app->response->getCookies()->add($cookie);


            $this->updateAll(['logintime'=>time(),'loginip'=>ip2long(Yii::$app->request->userIp)],'adminuser = :user',[':user'=>$this->adminuser]);
            return (boolean)$session['admin']['isLogin'];
        }
        return false;
    }

    public function seekpass($data){
        $this->scenario = "seekpass";
        if($this->load($data) && $this->validate()){

             //做点有意义的事
            $time = time();
            $token = $this->createToken($data['Admin']['adminuser'], $time);

            $mailer = Yii::$app->mailer->compose('seekpass', ['adminuser' => $data['Admin']['adminuser'], 'time' => $time, 'token' => $token]);
            $mailer->setFrom("chizhiyiheng1989@163.com");
            $mailer->setTo('905766491@qq.com');
            $mailer->setSubject("商城-找回密码");
            if ($mailer->send()) {
                return true;
            }
        }
        return false;
    }

    public function createToken($adminuser, $time)
    {
        return md5(md5($adminuser).base64_encode(Yii::$app->request->userIP).md5($time));
    }

    public function changePass($data){
        $this->scenario = "changepass";
        if($this->load($data) && $this->validate()){
            return $this->updateAll(['adminpass'=>md5($this->adminpass)],'adminuser=:user',[':user'=>$this->adminuser]);
        }
        return false;
    }

    public function reg($data){
        $this->scenario = 'adminadd';
        if($this->load($data) && $this->validate()){
            $this->adminpass = md5($this->adminpass);
            $this->createtime = time();
            if($this->save(false)){
                return true;
            }
            return false;
        }
        return false;
    }

    public function changeemail($data){
        $this->scenario = 'changeemail';
        if($this->load($data) && $this->validate()){
            return (bool)$this->update(['adminemail'=>$this->adminemail,'adminuser=:user',[':user'=>$this->adminuser]]);
        }
        return false;
    }
}